/**
 * @(#)ApkFilterController.java 2016年12月9日
 * Copyright(C) 2016 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.action;

import org.jsbd.boss.common.ConstantsCmp;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.dao.IChannelApkLimitDAO;
import org.jsbd.boss.dao.IWeiboBrushConfigDAO;
import org.jsbd.boss.domian.ChannelApkConfigTimeModel;
import org.jsbd.boss.domian.weibo.WeiboBrushConfig;
import org.jsbd.boss.service.IChannelLimitService;
import org.jsbd.boss.util.CacheKey;
import org.jsbd.boss.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 *
 *
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Controller
@RequestMapping("/weiboBrush")
public class WeiboBrushController {
    public static final Logger LOGGER = LoggerFactory.getLogger(WeiboBrushController.class);

    @Resource
    private IWeiboBrushConfigDAO brushConfigDAO;


    @Resource
    private RedisClient redisClient;


    @Resource
    private IChannelApkLimitDAO channelApkLimitDAO;


    @Resource
    private IChannelLimitService limitService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String list(Model model) {
        return "weiboBrush/list";
    }

    @RequestMapping(value = "list", params = "json")
    @ResponseBody
    public List<WeiboBrushConfig> queryList(DataGridModel page, WeiboBrushConfig config) throws Exception {
        Integer isUseful = config.getIsUseful();
        if (isUseful != null && isUseful == -1) {
            config.setIsUseful(null);
        }
        return brushConfigDAO.selectConfigList(config);
    }


    @RequestMapping(value = "{id}/update", method = RequestMethod.GET)
    public String update(WeiboBrushConfig config, Model model) {
        model.addAttribute("config", brushConfigDAO.selectConfig(config));
        return "weiboBrush/update";
    }

    private void initLimit(String code, Integer dayLimit) {
        try {
            limitService.getTimeLimit(code, dayLimit);
            limitService.getTimeRetentModelByMark(code);
        } catch (Exception e) {
            LOGGER.error("正态模型没有配置", e);
        }
    }

    private void updateLimit(String code, Integer dayLimit) {
        try {
            ChannelApkConfigTimeModel model = new ChannelApkConfigTimeModel();
            model.setMark(code);
            model.setSpDayLimit(dayLimit);
            model.setType(ConstantsCmp.add);
            channelApkLimitDAO.updateTimeModel(model);
        } catch (Exception e) {
            LOGGER.error("正态模型没有配置", e);
        }
    }

    @RequestMapping(value = "{id}/update", method = RequestMethod.POST)
    public void update(HttpServletResponse response, WeiboBrushConfig config) {
        try {
            Integer isLimit = config.getIsLimit();
            if (isLimit != null && isLimit == 1) {
                updateLimit(config.getCode(), config.getTaskNum());
            }
            brushConfigDAO.updateRecord(config);
            ResponseUtils.responseSuccess(response);
        } catch (Exception e) {
            ResponseUtils.responseFailure(response);
        }
    }

    @RequestMapping(value = "{id}/delete", method = RequestMethod.POST)
    public void del(HttpServletResponse response, WeiboBrushConfig config) {
        try {
            brushConfigDAO.deleteConfig(config);
            ResponseUtils.responseSuccess(response);
        } catch (Exception e) {
            ResponseUtils.responseFailure(response);
        }
    }

    @RequestMapping(value = "add", method = RequestMethod.GET)
    public String add() {
        return "weiboBrush/add";
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public void add(HttpServletResponse response, WeiboBrushConfig config) {
        try {
            long brushId = redisClient.incrementBy(CacheKey.B_WEIBO_BRUSH_CODE, 1);
            config.setCode("B_" + DateUtil.getCurrentDateStr() + String.format("%1$08d", brushId));
            config.setIsUseful(0);
            brushConfigDAO.insert(config);
            Integer isLimit = config.getIsLimit();
            if (isLimit != null && isLimit == 1) {
                initLimit(config.getCode(), config.getTaskNum());
            }

            ResponseUtils.responseSuccess(response);
        } catch (Exception e) {
            ResponseUtils.responseFailure(response);
        }
    }


    @RequestMapping(value = "channgeStatus")
    public void channgeStatus(WeiboBrushConfig config, HttpServletResponse response) {
        try {
            WeiboBrushConfig weiboBrushConfig = brushConfigDAO.selectConfig(config);
            weiboBrushConfig.setIsUseful(config.getIsUseful());
            brushConfigDAO.updateRecord(weiboBrushConfig);
            ResponseUtils.responseSuccess(response);
        } catch (Exception e) {
            LOGGER.error("出错", e);
            ResponseUtils.responseFailure(response);
        }
    }

}
